The PHP Snippet Compilation for Beginners
Date: 0101-01-01T00:00:00+00:00
Keywords: PHP; beginner; easy; webprogramming; MySQL; examples; ebook; free
Description: The PHP Snippet Compilation for Beginners. A Free Ebook from level67.wordpress.com. This version contains 30 easy source snippets, ready to use. Save it, and keep it in your source library. A good reference for beginners starting PHP webprogramming. A continuing project, to be updated. This ebook is basicly the content from my blog; http://level67.wordpress.com/ Here you should be able to find newest version of this ebook, in both the E-pub, and AZW3 formats.

The PHP snippet compilation

level67.wordpress.com

For beginners

><><><><><><><><><><><

Version 1.00
20150829

Table of Contents

How to INSTALL a LAMP stack
How to CREATE a DATABASE and a TABLE
How to INSERT into a TABLE part I
How to INSERT into a TABLE part II
How to SELECT from a TABLE part I
How to SELECT from a TABLE part II
How to DELETE from a a TABLE
How to UPDATE a row in a TABLE
How to ORDER the output from a TABLE
How to SELECT like from a TABLE
How to use $_POST[''] to read inputs from a FORM
How to use IF in PHP, simple
How to use IF and ELSE in PHP, simple
How to DELETE all data from a TABLE, without deleting the structure
How to CREATE a RANDOM password at any length
How to use PAX for making BACKUP
How to make automated BACKUP using PAX and CRONTAB
How to CREATE a textfile and write to it
How to READ from a textfile
How to CREATE a textfile and write to it, CONVERTING newline's
How to COPY af file and RENAME it as you go
How to DELETE (unlink) a file
How to make Choice with RADIOBUTTONS
How to show a RANDOM piece of graphic on every LOAD of a page
How to REDIRECT in PHP, using JAVASCRIPT in PHP
How to Strip off TAGS from text
How to CREATE a directory using PHP
How to CREATE a file using PHP
How to READ a file using PHP
How to CREATE a zip archive using PHP

About this E-book

How to INSTALL a LAMP stack.

TAGS: LAMP, WAMP, MAMP, stack, install, easy

In this blogposting I will install a LAMP stack.
If you already have MySQL and Apache with PHP running on your computer, then that is fine. You can skip this, and go to the following posts, where we will look at using PHP for handling a MySQL database.

The LAMP stack I use, is a download from bitnami.com, I use it because it's a very simple and easy way to do it. Fast.
You can also find a stack for Windows (WAMP) and for Mac (MAMP) on the same website. The installation is very similar, so you will gain from reading this post anyway.
The stack contains several programs, additions to PHP as you can see on the screenshot, number four, below. But in these blogs we will be looking closer at the Apache webserver, the MySQL database server, PHP serverside scripting language, and phpMyAdmin, the last being a manager for the MySQL database, running in your browser.
First we go to the bitnami site and download the LAMP stack here:
https://bitnami.com/stack/lamp

Okay, and now that you have downloaded that, then let us install it.
Find the downloaded file and double-click it. If your installer doesn't start, and if you are in Linux, then try right-click the file and choose “properties”. Here you choose the “Permission” tab, and tick the checkbox saying “Allow executing file as program”. Now try double-click the file again. This time the installer should start.
You will see something like this:

After the installation, you can find the directory where the stack is installed. This can be “/home/you/lampstack” depending on you choice during installation. Here is the file named “manager-linux-x64.run”, (for the 64bit Linux version). Double-clicking it will start the manager, and here you shall select the “Manage Servers” tab. You see MySQL and Apache in a listview. Just go and click at the bottom, on the button “Start All”. It will take a little while. Then the servers should get a green light, meaning they are running. If they fail, try starting them again.

If you can't get any contact to the stack in your browser after installing, and running the manager. Then try writing the URL with the port number 8080, like this;
http://127.0.0.1:8080/

So, now you have installed the stack we can start using it. You can put your webcontent in the webfolder, like here;
/home/you/lampstack/apache2/htdocs

Browse into the MySQL pages on this blog, here you will learn to do great things with MySQL, using PHP.
First posting is about how to CREATE the Database and how to CREATE the table with columns, using PHP and SQL. Then we have a database and a table with five fields ready to write into, as you will learn to do in the following postings. If you follow my postings we will build a project, that is to be used throughout these pages, at least the pages about MySQL, using PHP.

To the top ^

How to CREATE a database and a table using PHP.

TAGS: SQL, CREATE, PHP, MySQL

I assume that you have downloaded and installed the LAMPstack (or WAMP or MAMP), as explained in the first blogposting. Now follow these steps to setup a new database using PHP code.

The code we will use for creating the database is this:

CREATE DATABASE IF NOT EXISTS dbname;

And the code for creating the table is like this:

CREATE TABLE IF NOT EXISTS contacts
(
AutoID int NOT NULL AUTO_INCREMENT ,
Lastame varchar(255),
Firstname varchar(255),
EmailAddr varchar(255),
WebAddr varchar(255),
primary key(AutoID)
);

The first line will create the database for us. You can change the name of your database to what you like. With the second block we create the table, in the DB, for our contacts. In this table we will create some fields for holding info of our contacts, with firstname, lastname, emailaddress, and webaddress. These fields are set to be able to hold upto 255 characters. Also I make a field named “AutoID” that is giving each entry a number for reference. These numbers will be incrementing by 1 (one) for every new table row.

So our PHP document will look like this:

<?php

$SQL_string = "CREATE DATABASE IF NOT EXISTS dbname";

$con = mysqli_connect('127.0.0.1', 'root', 'Your_Password', '');
if (mysqli_connect_errno()) {
echo 'Error connecting to DB';
}

$dostuff = mysqli_query($con, $SQL_string);

if(! $dostuff ) {
echo 'Error in executing query';
}
mysql_close($con);
echo 'Success! Your database have been created!';

?>

If you get an error connecting to the DB then it is most likely the password. Notice that the last place, where you see '' is kept empty, that is where the name of your database will be, when using this connection-string later, calling an existing database.

And now I will create the table, with five fields. Like so;

$con = mysqli_connect('127.0.0.1', 'root', 'Your_Password', 'dbname');
if (mysqli_connect_errno()) {
echo 'Error connecting to DB';
exit();
}

$SQL_string = "
CREATE TABLE IF NOT EXISTS contacts
(AutoID int NOT NULL AUTO_INCREMENT,
Lastname varchar(255),
Firstname varchar(255),
EmailAddr varchar(255),
WebAddr varchar(255),
primary key(AutoID))";

$dostuff = mysqli_query($con, $SQL_string);

if(! $dostuff ) {
echo 'Error in executing query';
exit();
}

mysql_close($con);

echo 'Success! Your table have been created!';
?>

Unknown
Notice, that this time, the name of the database is existing, and therefor written in the connection-string.
Okay. Now you have a database with a table, ready to fill with all your contacts. In the next blog I will continue with an example showing how to write information to our new database.

To the top ^

How to INSERT into a table, part I.

TAGS: SQL, INSERT, PHP, MySQL

In the last posting, I was showing you how to CREATE a database, and a table to go with it.
Now we will look at how to populate that database, with entries read from a form on a webpage.

First lets have a look at the HTML.

<HTML>
<BODY>

<FORM METHOD='POST' ACTION='readmyform.php'>
<TABLE>
<TR>
<TD>Firstname: </TD>
<TD><INPUT TYPE='text' NAME='firstname'></TD>
</TR>

<TR>
<TD>Lastname: </TD>
<TD><INPUT TYPE='text' NAME='lastname'></TD>
</TR>

<TR>
Unknown
<TD>Email Address: </TD>
<TD><INPUT TYPE='text' NAME='emailaddr'></TD>
</TR>

<TR>
<TD>Homepage Address: </TD>
<TD><INPUT TYPE='text' NAME='webaddr'></TD>
</TR>

<TR>
<TD></TD>
<TD><INPUT TYPE='submit' VALUE='Okay, write to DB'></TD>
</TR>

</TABLE>
</FORM>
</BODY>
</HTML>

And now the PHP.

<?php
// Here we read the inputs from the Form
$Firstname = $_POST['firstname'];
$Lastname = $_POST['lastname'];
$EmailAddr = $_POST['emailaddr'];
$WebAddr = $_POST['webaddr'];

// Now lets connect to the database
$con = mysqli_connect('127.0.0.1', 'root', 'Your_Password', 'dbname');

if (mysqli_connect_errno()) {
> echo "Error connecting to DB";
}

// Here we compose the SQL string
$SQL_String = "INSERT INTO contacts
(Lastname, Firstname, EmailAddr, WebAddr)
VALUES ('$Lastname', '$Firstname', '$EmailAddr', '$WebAddr')";
// Execute the query – write to the DB
$dostuff = mysqli_query($con, $SQL_String);

if(! $dostuff ) {
echo 'Error in executing query';
exit();
}

mysqli_close ($con);

echo “Success, you have now written a row to your table”;
?>

Save the above code in two files. The first you can name 'myform.html', copy and paste the HTML code from above into that file. Then make another file and name it 'readmyform.php', put the PHP code from above into that file and save it.
Now open your browser and write this URL:
http://127.0.0.1/myform.html

Fill out the formfields and click on the submit button. Now you should see the line “ Success, you have now written a row to your table”.
Look at your database via phpMyAdmin. You should see the entry in the database. Remember to change the “Your_Password”, from the string connecting to the DB, into your password.

In the next posting we will look at the same code. But we will make things a little easier – I will show you how to have everything in the same file, both the HTML and the PHP coding.

To the top ^

How to INSERT into a table, part II.

TAGS: SQL, INSERT, PHP, MySQL

Okay. So in the last posting I showed you how to read input from a form in a HTML page, and write that input to a database. Now I will show how to make everything happen in a single file. Making things a little easier and less messy.

Take all the following code, (both the HTML and the PHP), and paste it into a file named 'mynewpage.php'.

<?php

if (isset($_POST['readme'])) {
doread();
}

function doread() {

// Here we read the inputs from the Form
$Firstname = $_POST['firstname'];
$Lastname = $_POST['lastname'];
$EmailAddr = $_POST['emailaddr'];
$WebAddr = $_POST['webaddr'];

// Now lets connect to the database
$con = mysqli_connect('127.0.0.1', 'root', 'Your_Password', 'dbname');

if (mysqli_connect_errno()) {
echo "Error connecting to DB";
}

// Here we compose the SQL string
$SQL_String = "INSERT INTO contacts (Lastname, Firstname, EmailAddr, WebAddr) VALUES ('$Lastname', '$Firstname', '$EmailAddr', '$WebAddr')";
// Execute the query – write to the DB
$dostuff = mysqli_query($con, $SQL_String);

if(! $dostuff ) {
echo 'Error in executing query';
exit();
}

mysqli_close ($con);
echo 'Success, you have now written a row to your table';

}
?>

<HTML>
<BODY>

<FORM METHOD='POST' ACTION='mynewpage.php'>
<TABLE>
<TR>
<TD>Firstname: </TD>
<TD><INPUT TYPE='text' NAME='firstname'></TD>
</TR>

<TR>
<TD>Lastname: </TD>
<TD><INPUT TYPE='text' NAME='lastname'></TD>
</TR>

<TR>
<TD>Email Address: </TD>
<TD><INPUT TYPE='text' NAME='emailaddr'></TD>
</TR>

<TR>
<TD>Homepage Address: </TD>
<TD><INPUT TYPE='text' NAME='webaddr'></TD>
</TR>

<TR>
<TD></TD>
<TD><INPUT TYPE='submit' NAME='readme' VALUE='Okay, write to DB'></TD>
</TR>

</TABLE>
</FORM>
</BODY>
</HTML>

Yes. It's as simple as that.
The same code, I have only changed a few things.
First as you see in the beginning we make use of isset. This will be useful later when we want to make different things happen in this same page. You may have seen pages with more than one submit button. Like a “Write” and “Edit”. These make use of the same <FORM>, but two separate submit buttons. So here at the top of the script we call a function depending on the name of the submit button pressed by the user. It's like this:

if (isset($_POST['readme'])) {
doread();
}

All that happens here, is that we check for the button named “readme”, and if it is submitted, then we call the function “doread”.
So we made a small change in the HTML FORM;

<INPUT TYPE='submit' NAME='readme' VALUE='Okay, write to DB'>

Naming the submit button, so we then can recognize the button being clicked. Notice also that the ACTION in the <FORM> now is pointing at the page itself.

Also we put all the existing PHP code into one function, that is done by putting the code inside two curly brackets. It look like so:

function doread() {
//.. all our code for this submit here ..
}

In the next blogposting I will show how to read your newly written input from the database.

To the top ^

How to SELECT from a table, part I.

TAGS: SQL, SELECT, PHP, MySQL

Welcome back.
In the last posting I tried to explain how to write some values taken from a HTML form, into a database that we created in an earlier post.

So, I expect that you have created the Database and the Table, and that you have populated the database with some contacts. Because now we will be reading those contacts.
Below here is the simple PHP code that does one simple thing – it reads the First and Last names of alle the contacts, and make their name a link, linking to their emailaddress, as found in the database.

Copy the below code into a file, name it 'read.php', then call it in your browser; http://127.0.0.1/read.php

<?php
$con = mysqli_connect('127.0.0.1', 'root', 'Your_Password', 'dbname');

if (mysqli_connect_errno()) {
echo 'Error connecting to DB';
}

$SQL_String = mysqli_query($con, 'SELECT * FROM contacts ORDER BY AutoID ASC');

while($row = mysqli_fetch_array($SQL_String)) {
echo "<A HREF='mailto:" .
$row['EmailAddr'] . "'>" .
$row['Firstname'] . " " .
$row['Lastname'] .
"</A><BR />";
}

?>

So, now you can list all the entries in the database, and in the example given here, you can see how you can build a link using some of the values.
Because there are several entries in the database we make use of WHILE. So we can loop through all rows, and display them on a webpage. In the next example we will look at selecting only one entry from the database. Hang on for the next blog. We continue learning about reading from a SQL database using PHP. SELECT.

To the top ^

How to SELECT from a table, part II.

TAGS: SQL, SELECT, PHP, MySQL

As promised in the last post we are now to select a single row from the database, and display values from that row. We will make a list of our contacts in a SELECT box. Then we can choose one person, and retrieve all the information we have about that person, from our database. So, lets get started. We have to make the webpage, and by making the SELECT we will use what we learned in the previous post – reading through all content.
Put the code in a file named 'select.php' and call it in your browser.
http://127.0.0.1/select.php

<?php

if (isset($_POST['readme'])) {
doread();
}

function doread() {
// This function is called when selecting a person from the SELECT
$IDnummer = $_POST['contact'];

$con = mysqli_connect('127.0.0.1', 'root', 'Your_Password', 'dbname');
if (mysqli_connect_errno()) {
echo 'Error connecting to DB';
}
$SQL_String = mysqli_query($con, 'SELECT * FROM contacts WHERE AutoID=' . $IDnummer . '');
$row = mysqli_fetch_array($SQL_String);

echo "You selected: " . $row['Firstname'] . " " . $row['Lastname'];
mysqli_close ($con);
}

?>

<HTML>
<BODY>
<FORM METHOD='POST' ACTION=''>
<TABLE>
<TR>
<TD>Our contacts: </TD>
<TD><SELECT NAME='contact'><OPTION>Make your choice

<?php
// Here we populate the SELECT with our contacts
$con = mysqli_connect('127.0.0.1', 'root', 'Your_Passwords', 'dbname');

if (mysqli_connect_errno()) {
echo 'Error connecting to DB';
}

$SQL_String = mysqli_query($con, 'SELECT * FROM contacts ORDER BY AutoID ASC');

while($row = mysqli_fetch_array($SQL_String)) {
echo '<OPTION VALUE=' .
$row['AutoID'] . '>' .
$row['Firstname'] . ' ' .
$row['Lastname'];
}
mysqli_close ($con);
?>

</SELECT>
</TD>
</TR>
<TR>
<TD></TD>
<TD><INPUT TYPE='submit' NAME='readme' VALUE='Okay, select'>

</TR>
</TABLE>
</FORM>
</BODY>
</HTML>

As you can see, the code handling the database, inside the HTML, is looping through the rows of your table. This is for populating the SELECT.
Then, when submitting our choice, we call the function 'doread' in the top of the page. And here we only extract one row from the table. That one row is selected by its AutoID. What is done in the SQL query;

.. SELECT * FROM contacts WHERE AutoID= . $IDnummer ..

We have, just before that, read the AutoID from the SELECT, that was done by;

$IDnummer = $_POST['contact'];

Where 'contact' is the name of the SELECT field.
And with that, we get the value of the person in the contact list that we chose, so now we can dig out that row from the table.

See the next posting for mere more exciting MySQL stuff, using PHP.

To the top ^

How to DELETE from a table.

TAGS: SQL, DELETE, PHP, MySQL

In this posting we will be economical and recycle most of the code, most of the HTML anyway.
In this posting, I will show how you can delete a row from the table 'contacts', that we have created earlier, and populated with information about our contacts. Put all the code in a file named 'delete.php'.
So, lets go.
http://127.0.0.1/delete.php

<?php

if (isset($_POST['readme'])) {
dodelete();
}

function dodelete() {

$IDnummer = $_POST['contact'];

$con=mysqli_connect('127.0.0.1', 'root', 'Your_Password', 'dbname');
if (mysqli_connect_errno()) {
echo "Error connecting to DB";
}
mysqli_query($con, 'DELETE FROM contacts WHERE AutoID=' . $IDnummer . '');
mysqli_close($con);
}
echo 'Success, you have deleted a row from your table.';

?>

<HTML>
<BODY>
<FORM METHOD='POST' ACTION=''>
<TABLE>
<TR>
<TD>Our contacts: </TD>
<TD><SELECT NAME='contact'><OPTION>Make your choice

<?php
$con = mysqli_connect('127.0.0.1', 'root', 'Your_Password', 'dbname');

if (mysqli_connect_errno()) {
echo 'Error connecting to DB';
}

$SQL_String = mysqli_query($con, 'SELECT * FROM contacts ORDER BY AutoID ASC');

while($row = mysqli_fetch_array($SQL_String)) {
echo '<OPTION VALUE=' .
$row['AutoID'] . '>' .
$row['Firstname'] . ' ' .
$row['Lastname'];
}
mysqli_close ($con);
?>

</SELECT>
</TD>
</TR>
<TR>
<TD></TD>
<TD><INPUT TYPE='submit' NAME='readme' VALUE='Okay, delete'>

</TR>
</TABLE>
</FORM>
</BODY>
</HTML>

What happens is that we populate a SELECT, with all entries from your table. Here you can make a selection and by clicking the submit, the AutoID of the row is read by the PHP program, that then will delete the entire row from the table.
The thing doing it, is by query the SQL string. The commando is 'DELETE', and we specify the exact row, by the AudoID, that we read from the SELECT. Like this:

DELETE FROM contacts WHERE AutoID=$IDnummer

When you delete this way you wont get any warnings. The row is instantly deleted and gone. Look at the SELECT again, the entry you wanted to delete should no longer be in the list.

Now things are rolling. You soon have an arsenal of handy SQL knowledge. The next blogposting here on this site will be about updating an existing row in your table. Imagine one of your contacts will change her emailaddress. You will then have to change it in your contact database. Updating.

Look out for the next post.

To the top ^

How to UPDATE a row in a table.

TAGS: SQL, UPDATE, PHP, MySQL

Okay. Next step in this short serie is to make an UPDATE in our existing data. If you copy the code below into a PHP document you should be able to UPDATE your table rows. Put the code in a file named 'update.php' and call it in your browser.
http://127.0.0.1/update.php

<?php

if (isset($_POST['updaterow'])) {
doedit();
}

if (isset($_POST['readme'])) {
doread();
}

function doedit() {

$IDnummer = $_POST['idnummer'];
$Firstname = $_POST['firstname'];
$Lastname = $_POST['lastname'];
$EmailAddr = $_POST['emailaddr'];
$WebAddr = $_POST['webaddr'];

$con = mysqli_connect('127.0.0.1', 'root', 'Your_Password', 'dbname');

if (mysqli_connect_errno()) {
echo "Error connecting to the DB";
}

$SQL_String = "UPDATE contacts
SET Firstname='$Firstname',
Lastname='$Lastname',
EmailAddr='$EmailAddr',
WebAddr='$WebAddr'
WHERE AutoID='" . $IDnummer . "'";

mysqli_query($con, $SQL_String);
mysqli_close ($con);

}

function doread() {

$IDnummer = $_POST['contact'];

echo "<HTML><BODY><FORM METHOD='POST' ACTION=''>
<INPUT TYPE='hidden' NAME='idnummer' VALUE='" . $IDnummer . "'>
<TABLE>";

$con = mysqli_connect('127.0.0.1', 'root', 'Your_Password', 'dbname');
if (mysqli_connect_errno()) {
echo 'Error connecting to DB';
}
$SQL_String = mysqli_query($con, 'SELECT * FROM contacts WHERE AutoID=' . $IDnummer . '');
$row = mysqli_fetch_array($SQL_String);

echo "<TR>
<TD>Firstname: </TD>
<TD><INPUT TYPE='text' NAME='firstname' VALUE=" . $row['Firstname'] . "></TD>
</TR>";

echo "<TR>
<TD>Lastname: </TD>
<TD><INPUT TYPE='text' NAME='lastname' VALUE=" . $row['Lastname'] . "></TD>
</TR>";

echo "<TR>
<TD>EmailAddr: </TD>
<TD><INPUT TYPE='text' NAME='emailaddr' VALUE=" . $row['EmailAddr'] . "></TD>
</TR>";

echo "<TR>
<TD>WebAddr: </TD>
<TD><INPUT TYPE='text' NAME='webaddr' VALUE=" . $row['WebAddr'] . "></TD>
</TR>";

echo "<TR>
<TD></TD>
<TD><INPUT TYPE='submit' NAME='updaterow' VALUE='Update row'></TD>
</TR>";

mysqli_close ($con);
}

?>

<HTML>
<BODY>
<FORM METHOD='POST' ACTION=''>
<TABLE>
<TR>
<TD>Our contacts: </TD>
<TD><SELECT NAME='contact'><OPTION>Make your choice

<?php
$con = mysqli_connect('127.0.0.1', 'root', 'Your_Password', 'dbname');

if (mysqli_connect_errno()) {
echo 'Error connecting to DB';
}

$SQL_String = mysqli_query($con, 'SELECT * FROM contacts ORDER BY AutoID ASC');

while($row = mysqli_fetch_array($SQL_String)) {
echo '<OPTION VALUE=' . $row['AutoID'] . '>' . $row['Firstname'] . ' ' . $row['Lastname'];
}
mysqli_close ($con);
?>

</SELECT>
</TD>
</TR>
<TR>
<TD></TD>
<TD><INPUT TYPE='submit' NAME='readme' VALUE='Okay, select'>

</TR>
</TABLE>
</FORM>
</BODY>
</HTML>

The last part of the code is what we actually see first.
It will populate a SELECT with our contacts. We also get a submitbutton with the caption "Okay, select".

When we click that button we go to the function doread(). Here we read what AutoID number was selected, it is the number of the contact we have selected, so when we have that we can read all info from the database in the row containing the values for our selected contact.
We get the values presented in text-fields, so we can change the text as we like. Then when we click this button, we read (in function doedit()) the inputs from the textfields on the HTML form, we put the content into some variables, in this example five variables.

We now construct the SQL, where we update the database fields, with the inputs from the form contained in the variables.
We make sure to put things in the right place, and we do that by stating that we want to update the fields in the row WHERE AutoID is the $IDnummer. That was the value that we read from the 'Hidden' form field.

Next I will look at how to make order in the search results from querying you database table.

To the top ^

How to ORDER the output from a table.

TAGS: SQL, ORDER BY, PHP, MySQL

Now I will show how to ORDER the output from a query. We order the output by choosing a field from our table that we want to order the query after. In our example database this can be for example the AutoID field, swhere each row is given a unique number. We can also order the listing we make by ORDER BY Lastname, or whatever we have in our database.

In this example we ORDER BY AutoID. We can choose to order from low numbers to higher numbers (older to newer). Or we can make the listing go from high numbers to lower numbers (newer to older rows).
This is done by the ASC or DESC, as you see in the code below. If you use ASC/DESC in fields containing characters it will order from A-Z (ASC), or Z-A (DESC).

Now create a .php file and name it 'orderby.php'. Paste in the code below, and then open it in your browser.
http://127.0.0.1/orderby.php

<?php
$con = mysqli_connect('127.0.0.1', 'root', 'Your_Password', 'dbname');

if (mysqli_connect_errno()) {
echo 'Error connecting to DB';
}

$SQL_String = mysqli_query($con, 'SELECT * FROM contacts ORDER BY AutoID DESC');

while($row = mysqli_fetch_array($SQL_String)) {
echo '<OPTION VALUE=' . $row['AutoID'] . '>' . $row['AutoID'] . ': ' . $row['Firstname'] . ' ' . $row['Lastname'];
}
mysqli_close ($con);
?>

You will see the AutoID number printet before the name of your contacts in the table we use in our examples.
Then try to change the SQL string in this example, and write ASC instead of DESC. Load the page again, and now, in the listing, you should see your contacts listed from the lowest number to the higher numbers.

To the top ^

How to SELECT LIKE from a table.

TAGS: SQL, LIKE, PHP, MySQL

Now, in this post, we will look at the SQL operator 'LIKE'.
No, it's not about facebook, but it's about finding content from your database using words, or part of words, for searching, where you will get results, containing your seachphrase.

Imagine searching for '%angel%'. With LIKE you will get 'Angel', 'Angels', 'Los Angeles', 'Archangel', and so on.

If you use this; '%ing' you will search for word ending in 'ing'.
On the other hand, if you do this; 'be%' you search for words beginning with 'be'.

If you want to find all words with the phrase 'tt', you can do this; '%tt%'. Then your result will be words with 'tt' somewhere in it. Like getting, lotto, betting, setting, etc.

Her is an example that you can try, using the database you have created in the beginning of these blogs.

<?php
$con = mysqli_connect('127.0.0.1', 'root', 'Your_Passwords', 'dbname');

if (mysqli_connect_errno()) {
echo 'Error connecting to DB';
}

$SQL_String = mysqli_query($con, 'SELECT * FROM contacts WHERE Lastname LIKE '%sen'');

while($row = mysqli_fetch_array($SQL_String)) {
echo $row['AutoID'] . ': ' .
$row['Firstname'] . ' ' .
$row['Lastname'] . '<BR />';
}
mysqli_close ($con);
?>

Notice that if your table does not contain any Lastnames that ends with the string 'sen', then you will not get any result. Either try to add some rows to the table, as seen in an ealier posting, or change the %LIKE% pattern in the SQL string, in the above codesnippet.

Now we will go from handling databases to look at other things that you can do with PHP. In the next blog I will give an example on how to generate random passwords.

To the top ^

How to use $_POST[] to read inputs from a FORM

TAGS: PHP, POST, FORM

In this posting we will learn how to read inputs from a FORM on a webpage.
You can use the $_POST[''] to read all kinds of values from FORMS, f.eks; text, password, hidden, checkbox, radio, select, submit. As the name say it reads POSTs from FORMs where the METHOD is set to 'POST'. You can read the returned value in to a variable easily, just make something like this;

$myvar = $_POST['myfield'];

You have then put the value of 'myfield', submitted from your FORM, in your variable $myvar, and you can use it in your code elsewhere.

The following code simply show a webpage with two textfields and a submitbutton. You can enter some names in the textfields and on submit you will see that the values are read from the form and send to the screen. This just shows how to read values from a form into variables. You can change and use it as you want, for example for entering names into our test database for our contacts.

<?php

if (isset($_POST['readme'])) {
showme();
}

function showme() {
$Firstname = $_POST['firstname'];
$Lastname = $_POST['lastname'];

echo "Your contact is: " . $Firstname . " " . $Lastname;
//exit();
}

echo "<HTML>
<BODY>
<FORM METHOD='POST' ACTION=''>
<TABLE>

<TR>
<TD>Firstname: </TD>
<TD><INPUT TYPE='text' NAME='firstname' VALUE='" . $Firstname . "'>
</TD>
</TR>

<TR>
<TD>Lastname: </TD>
<TD><INPUT TYPE='text' NAME='lastname' VALUE='" . $Lastname . "'>
</TD>
</TR>

<TR>
<TD></TD>
<TD><INPUT TYPE='submit' NAME='readme' VALUE='Okay, read me'>

</TR>
</TABLE>
</FORM>
</BODY>
</HTML>";
?>

In the next post I will use this example and show you how IF works in PHP.

To the top ^

How to use IF in PHP, simple

TAGS: PHP, if

In this posting we will look shortly on using IF in PHP scripts.
In the code below you can see three radiobuttons and a submitbutton. When you tick one of the radiobuttons and click the submit you will put a string into the variable $check that will be shown on submit.

We read the value using $_POST['question'], and with IF we assign a string to the variable, depending on wich radiobutton was the selected one.

In the radiobuttons on the HTML form field you can see that the values are set to 'yes', 'no' and 'unsure', this is what we look for. If you have other input types in your form, then what you look for are these VALUEs.

Here is the code.

<?php

if (isset($_POST['readme'])) {
checkme();
}

function checkme() {
$question = $_POST['question'];
if ( $question == 'yes' ) { $check = "You like PHP!"; }
if ( $question == 'no' ) { $check = "You don't like PHP?"; }
if ( $question == 'unsure' ) { $check = "You are unsure.."; }
echo $check;
}

echo "<HTML>
<BODY>
<FORM METHOD='POST' ACTION=''>
<TABLE>

<TR>
<TD>Do you like PHP: </TD>
<TD><INPUT TYPE='radio' NAME='question' VALUE='yes'> Yes</TD>
</TR>

<TR>
<TD></TD>
<TD><INPUT TYPE='radio' NAME='question' VALUE='no'> No</TD>
</TR>

<TR>
<TD></TD>
<TD><INPUT TYPE='radio' NAME='question' VALUE='unsure'> Unsure</TD>
</TR>

<TR>
<TD></TD>
<TD><INPUT TYPE='submit' NAME='readme' VALUE='Okay, try this'>

</TR>
</TABLE>
</FORM>
</BODY>
</HTML>";
?>

In the next post I will show you how IF/ELSE works in PHP.

To the top ^

How to use if and else in PHP, simple

TAGS: PHP, if, else

In this posting we will look shortly at IF and ELSE in PHP.
In the code below you can see two textfields and a simple checkbox and a submitbutton.

The content of the textfields are send to the screen when submitting, and if you have ticked the checkbox you will put a string into the variable $check. Then the whole page is send to the browser, and depending on if the box was ticked or not the message is shown along the firstname and lastname of your contact. If the box was checked we write a message into the variable, if not we just write an empty string.

We read the checkbox value using $_POST['member'], and with IF we simply assign a string to the variable if the checkbox was ticked. In the checkbox HTML form field you can see that the value is set to 'on', this is what we look for. If you have other input types in your form, then what you look for are these VALUEs.
We also use ELSE. That is simply if the $_POST returns any thing else than 'on'.

Here is the code.

<?php

$Firstname = $_POST['firstname'];
$Lastname = $_POST['lastname'];
$Member = $_POST['member'];

if ( $Member == 'on' ) { $check = 'She is a member'; }
else { $check = ''; }

echo "<HTML>
<BODY>
<FORM METHOD='POST' ACTION=''>
<TABLE>

<TR>
<TD></TD>
<TD>Your contact is: " . $Firstname . " " . $Lastname . "<BR/>" . $check . "<BR /></TD>
</TR>

<TR>
<TD>Firstname: </TD>
<TD><INPUT TYPE='text' NAME='firstname' VALUE='" . $Firstname . "'>
</TD>
</TR>

<TR>
<TD>Lastname: </TD>
<TD><INPUT TYPE='text' NAME='lastname' VALUE='" . $Lastname . "'>
</TD>
</TR>

<TR>
<TD>Member: </TD>
<TD><INPUT TYPE='checkbox' NAME='member' VALUE='on'></TD>
</TR>

<TR>
<TD></TD>
<TD><INPUT TYPE='submit' NAME='readme' VALUE='Okay, select'>

</TR>
</TABLE>
</FORM>
</BODY>
</HTML>";
?>

That was about if and else for now.

To the top ^

How to DELETE all data from a TABLE, without deleting the structure.

TAGS: MySQL, Table, PHP, TRUNCATE

We return briefly to SQL'ing, to look at an example on how to delete all content in a table. ..
So now we have filled up our table 'contacts' with a lot of tests. We are ready to use the database for real things, and we want to delete every row in the table.
How is that done?
Here is a solution that deletes everything without messing up the structure.

$con=mysqli_connect('127.0.0.1', 'root', 'Your_Password', 'dbname');
if (mysqli_connect_errno()) {
echo "Error connecting to DB";
}
mysqli_query($con, 'TRUNCATE TABLE contacts');
mysqli_close($con);

As you see in the above example deleting all content is quite simple. Again SQL is our friend and provide the TRUNCATE with which we can clean out the table.

To the top ^

How to create a random password at any length.

TAGS: Random, Password, PHP

When you have users signing up to your website, you often want to generate a password for them. There are of course different ways to do this. I give you one here that works for me.

We make a function containing the code. In this example the function is called straight away, so you see the result instantly. But we can call it where we need it in our program, and get a password in return.

Like this:

$MyPass = generate_pass(6);

Now $MyPass holds the password just created, and can be used in the code.

<?php

function generate_pass($length) {
$charsstring = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789';
$countchars = mb_strlen($charsstring);

for ($i=0, $result=''; $i<$length; $i++) {
$index = rand(0, $countchars-1);
$pass = $pass . mb_substr($charsstring, $index, 1);
}
return $pass;
}

// Here we call the function, and get the new password back
$MyPass = generate_pass(6);
// Here we print the new password to the browser
echo "Hey! You have a password..";

echo $MyPass;
?>

I have removed I, l, L, i, o, O, 0, 1 from the list of numbers, and characters in the alphabet, to avoid mistakes.
Try to change the value from 6 into some other value giving the length of the password.

To the top ^

How to use PAX for making BACKUP

TAGS: Script, PAX, Shell, Linux

I will now like to tell you about PAX. I found in PAX a super commandline program for making backups. It takes a blink of the eye.
Before I turn off the machine for the day, I run backup scripts, for backing up the projects I have been working at during the day. To have a script for each project will really make it easier to remember to backup. Imagine having one for the website you currently work at, and the documents that you write on at the moment. Anything.

Use this line to download and install PAX

sudo apt-get install pax

There is full coverage of how to use the command if you look in the man page for PAX. Like so.

man pax

Here is the finished shell script that I use for a project.

cd /home/you/lampstack/apache2/htdocs/project
rm backup.pax
pax -wzf backup.pax .
cp backup.pax /home/you/backup/project/$(date +%F).pax

As you can see first we change directory into the project directory in our apaches htdocs directory. Here we start by removing the old backup. So that we don't back that up also. Now we let PAX make a package containing the whole directory content, including sub directories. When that is done we copy the backup package to another path, and change name of the .pax file into a package named todays date. (Like 2015-05-23.pax).
I believe that you can change and adapt this script in several ways. For an example add the hour to the package name, in case you may need to make more backups the same day. Also you can make it send the package to yourself as an attachment to an email, also from the same scripts. I have added the scripts to my contextmenu in the “Files” filemanager, using Nautilus-Actions Configuration Tool. You can delete the original backup package after copying it to the backup directory. But I don't – it is okay to keep it, as another backup, it will be deleted at next run anyway. The backup is not named the date until copied, that way it is easy to know what file to delete (rm) from the project directory (backup.pax).

To unpack the pax-file you can do like so:

cd /home/you/backup/project
pax -rzf 2015-05-23.pax

..but keep in mind to do it in a directory for the same purpose, if your backup contains more than a few files. Messy.

The options I use for writing and reading with PAX is;

-w Write files to the standard output in the specified archive format.
-f Specify archive as the pathname of the input or output archive
-z Use the gzip utility to compress (decompress) the archive
-r Read an archive file

Look out for the next posting. In that I will make even better use of PAX. We will execute the scripts on hourly schedule. Using Crontab.

To the top ^

How to make automated backup using PAX and Crontab

TAGS: Script, PAX, Shell, Linux, Crontab

If you found the last posting interesting, then I am sure you will like this one also.
We made a script that makes use of PAX, a commandline tool, for making backup of our projects.

Now we will make cronjobs for executing those scripts, so that we totally automize the backup procedures on our machine.

Lets recall our script..

cd /home/you/lampstack/apache2/htdocs/project
rm backup.pax
pax -wzf backup.pax .
cp backup.pax /home/you/backup/project/$(date +%F).pax

About crontab:
cron is the system process which will automatically perform tasks for you according to a set schedule. The schedule is called the crontab, which is also the name of the program used to edit that schedule.
(Cite from the website linked to at the end of this text).

Here is how the crontab entry looks if we want to execute our script every hour at 15 minutes past the hour.

15 * * * * /home/you/programs/scripts/cronjob_15.sh

Here we execute the same cronjob every workday of the week at 15 minutes past 8 (AM).

15 8 * * 1-5 /home/you/programs/scripts/cronjob_15.sh

A cronjob consist of 6 entries.

The first is Minutes (0-59)
The second is for hours (0-23)
Third is day of month (1-31)
Fourth entry is month (1-12)
Fifth is day of week (0-7)
Sixth is the path to the scriptfile to run.

Notice that in the fifth both 0 an 7 are Sundays.

Okay. So we have the information we need to build our automated backup scripts. I assume that we use the script as seen above. And that we use the second example of the cronjob also above here. Now we need to enter the job into the crontab. We can do that easliy. Write in the following in your terminal:

$ crontab -e

.. this will open the nano editor with the crontab config file.
I made two screenshots of the nano editor, you see those here:

As you can see, the bottomline in the second shot show our entry into the crontab.

When you have written the line into nano end by hitting CTRL-X. The nano editor will ask you if you want to save. Hit Y (yes), and then A (append).
Your cronjob should have been added now. You can check it by typing this:

cronjob -l

Remember to look at:

man crontab

Now your system will create a backup every workday at 15 minutes past 8 o'clock.

I have chosen a link for more information. Look at:
http://www.computerhope.com/unix/ucrontab.htm

To the top ^

How to create a textfile and write to it

TAGS: PHP, fopen, fwrite

Often you may need to write a file on your server. Maybe for some configuration, or maybe just to hold some information till later. Or simply your users need to create stuff, where a basic flat textfile does the job and does it good, small size, simplicity etc.

To write a textfile is very simple. Below here I give you a working example. Put the code in a file, name it 'writetext.php' and call it from you server. If the textfile does not exists the code will create it.

<?php
$String = "Hello World!\n\nThis is a test in creating a textfile, and writing to it.";

$mytextfile = fopen("file.txt", "a") or die("An error occurred. <A HREF='formmail.php'>Send webmaster a message</A>");

fwrite($mytextfile, $String);
fclose($mytextfile);

echo 'Super, you have created a file';

?>

fopen have following options;

'r' (read only, file pointer at the beginning)
'r+' (read and write, file pointer at the beginning)
'w' (write only, file pointer at the beginning, zero length file, create it if it does not exist)
'w+' (read and write, file pointer at the beginning, zero length file, create it if it does not exist)
'a' (write only, file pointer at the end, zero length file)
'a+' (read and write, file pointer at the end, zero length file)
'x' (write only, file pointer at the beginning, if exists, return FALSE)
'x+' (read and write, file pointer at the beginning, if exists, return FALSE)

You can put all kinds of things in a textfile. At a later post I will create an XML file, we will both write it, and read it. But we will use only this example (writing) for that. PHP have a module for handling XML, we will use that for reading the content.

Look out for that post, sign up for email notification when new things happens here on this blog.

Next: How to read from a textfile

To the top ^

How to read from a textfile

TAGS: PHP, fopen, fread

Okay. In the last post we saw how to create, and write to, a textfile. That is a good thing, but then – it will be nice to be able to read that file again, later.
How do we do that?
If you thought that is was simple to write, then look at this. It is also very simple to read from a textfile.

First we assign a variable the name of our file. Here just “file.txt”. If you need it, then remember a path.
Then we open the file for Read only ('r'). We read all content in to a new vaiable named $String, and we make sure to get all content by specifying the size of what we want to read. It could be 24, meaning 24 characters, but we don't know the size maybe, so here we want as many as the complete size of the file – shortly: filesize($filename) - All.

<?php
$filename = 'file.txt';
$file = fopen($filename, 'r');
$string = fread( $file, filesize($filename) );
echo $string;
?>

fopen have following options;

'r' (read only, file pointer at the beginning)
'r+' (read and write, file pointer at the beginning)
'w' (write only, file pointer at the beginning, zero length file, create it if it does not exist)
'w+' (read and write, file pointer at the beginning, zero length file, create it if it does not exist)
'a' (write only, file pointer at the end, zero length file)
'a+' (read and write, file pointer at the end, zero length file)
'x' (write only, file pointer at the beginning, if exists, return FALSE)
'x+' (read and write, file pointer at the beginning, if exists, return FALSE)

To the top ^

How to create a textfile and write to it, coverting newline's

TAGS: PHP, fopen, fwrite, nl2br, convert, newline, break

If we want to read the 'file.txt' that we created in the posting 'How to create a textfile and write to it', we will notice that the newlines that we inserted in the string, written to the file does not show in the webbrowser. So what can we do about that?
PHP comes with a small solution to that problem, it is called 'nl2br' (newline to break), it will convert the newline into a HTML
.
Here I show how to do this. It is very simple, we just add one line, the one that is highlighted in this snippet. Then we write HTML breaks instead of newlines.

<?php
$String = "Hello World!\n\nThis is a test in creating a textfile, and writing to it.";
$String = nl2br($String);

$mytextfile = fopen("file.txt", "a") or die("An error occurred. <A HREF='formmail.php'>Send webmaster a message</A>");

fwrite($mytextfile, $String);
fclose($mytextfile);

echo 'Super, you have created a file';

?>

This was a way to handle linebreaks.

To the top ^

How to COPY a file, and RENAME it as you go

TAGS: PHP, File, Copy, Rename

When you will like to copy a file, and maybe rename it at the same time, then you can use this very simple piece of code.

<?php
$sourcefile = 'file.txt';
$destinationfile = './backup/file.bak';
copy($sourcefile, $destinationfile)
?>

.. but I really like this next example, doing the error checking in same line as the Copy function, simply.
It happens by checking 'if' there is an error with the copy. ('!' - meaning NOT), so if NOT copying (caused by an error), then send an error message to the screen.

<?php
$sourcefile = 'file.txt';
$destinationfile = './backup/file.bak';
if (!copy($sourcefile, $destinationfile)) {
echo "Error trying to copy $sourcefile!";
}
?>

To the top ^

How to DELETE (unlink) a file.

TAGS: unlink, PHP, delete, file

In PHP deleting a file is called to 'unlink' a file.
The code is very simple. Here is an example of how to delete the file.txt that we made earlier. Notice that you will not get any warnings. If you need such, then you will have to make that yourself.

<?php
unlink('file.txt');
?>

If your file is not at the path of the PHP script, then remember to write the path to the file.

To the top ^

How to make Choice with RADIOBUTTONS

TAGS: Radio, PHP, Choice, Select

Here is shown two radio buttons and one submit button.
At submit the PHP code will check if one or the other was chosen, and then send your user to a page depending on the choice.

Put this in a PHP page:

<?PHP
if ($_POST['page'] == "level67") {
header('location:http://level67.wordpress.com/');
} elseif ($_POST["page"] == "wordpress") {
header('location:http://www.wordpress.com/');
}
?>

<HTML><BODY>
<FORM METHOD='POST' ACTION=''>
<INPUT TYPE='radio' NAME='page' VALUE='level67'> Level67<BR />
<INPUT TYPE='radio' NAME='page' VALUE='wordpress'> Wordpress<BR />
<INPUT TYPE='submit'><BR />
</FORM>
</BODY></HTML>

Call the page in your browser. You should see the radio buttons and the submit button. Make a choice and click on the submit. You are now sent either to the wordpress frontpage, or the frontpage of level67.

To the top ^

How to show a RANDOM piece of graphic on every LOAD of a page

TAGS: LAMP, WAMP, MAMP, stack, install, easy

This small example can be used for example in your header, showing a random banner every time a new page is loaded. Or a piece of graphic anywhere on your page.
Here I make use of five graphics. You should rename you graphics 'banner1.png', 'banner2.png', ... etc.

<IMG SRC='graphics/banner<?php echo(mt_rand(1,5)); ?>.png' />

As you see the number/name of the banner is 1-5 and is simply inserted between 'banner' and '.png'

This is a small stand-in for a slideshow, that may be what your are really needing, if you need effects or the images to come in a specific order. Notice that because this is randomly, you may see the same graphic come twice, nothing in any order, and it may not even show image number 3 the first 10 times..
But simple.

To the top ^

How to REDIRECT in PHP, using JAVASCRIPT.

TAGS: PHP, Javascript, Redirect

Javascript?
Yes!

I was looking for a way to redirect in PHP code to an URL, but I ended up using a small snip of javascript.
It looks like so:

?>
<script>location.assign("http://127.0.0.1/mypage.php");</script>
<?php

As you can see the page mypage.php is called by this snippet.
The little, but obvious trick is to use the close tag for the PHP code, then do the JS thing and then give the PHP start tag again, returning to the program.
Maybe not elegant, but it's working.

* Se also the post about Radiobuttons, for another way to redirect, using PHP.

To the top ^

How to Strip off TAGS from text

TAGS: PHP, remove tags, strip

For safety reasons you really should remove tags from the text that you users can input.
In PHP we have a nice little tool to strip off all HTML and PHP tags from any text. You can use it like in the example below, where the input from a textfield named 'form-subject' is read in a normal way with $_POST[''] and put in a variable named $subject. This is then parsed through the strip_tags and can then safely be written in to your database.

$subject = $_POST['form-subject'];
$subject = strip_tags($subject);

Or in short;

$subject = strip_tags($_POST['form-subject']);

This;

$teststreng = "<A HREF=''>Hello world</A><?php mysqli_query($con, $SQLstr); ?>";
$teststreng = strip_tags($teststreng);
echo $teststreng;

...will become this; 'Hello world'.

To the top ^

How to CREATE a directory using PHP

TAGS: Create, dir, mkdir, directory

<?php
if (isset($_POST['doit'])) {
$dirname = $_POST['dirname'];
$stien = getcwd();
mkdir ($stien . "/" . $dirname);
echo "Okay, directory: " . $stien . "/" . $dirname . " have been created"; exit();
}
?>

<HTML><BODY>
<TABLE>
<FORM METHOD='POST' ACTION=''>
<TR>
<TD>Give your directory a name</TD>
<TD><INPUT TYPE='text' NAME='dirname'></TD>
</TR>
<TR>
<TD>Click here to create directory: </TD>
<TD><INPUT TYPE='submit' NAME='doit' VALUE='Create directory'></TD>
</TR></FORM>
</TABLE>
</BODY></HTML>

This example shows how easy it is to create a directory using PHP.
Just put the above code into a PHP file and call it from your webserver.

To the top ^

How to CREATE a file using PHP

TAGS: PHP, Textfile

<?php
if (isset($_POST['doit'])) {
$filename = $_POST['filename'];
$stien = getcwd();
$file = fopen($stien . "/" . $filename, 'w');
fclose();
echo "Finished"; exit();
}
?>

<HTML><BODY>
<TABLE>
<FORM METHOD='POST' ACTION=''>
<TR>
<TD>Give your file a name, <BR/>remember extension (.txt)</TD>
<TD><INPUT TYPE='text' NAME='filename'></TD>
</TR>
<TR>
<TD>Click here to create file: </TD>
<TD><INPUT TYPE='submit' NAME='doit' VALUE='Create textfile'></TD>
</TR></FORM>
</TABLE>
</BODY></HTML>

This is a very simple example on how to create an empty textfile. .. or any flatfile. Simply put the above code into a .php file and call it from you webserver.

To the top ^

How to READ a file using PHP

TAGS: fopen, fread, read file, textfile

<?php
if (isset($_POST['doit'])) {
$viewme = 'file.txt';
$file = fopen($viewme, 'r');
$string = fread( $file, filesize($viewme) );

echo "<TEXTAREA ROWS='24' COLS='80'>";
echo $string;
echo "</TEXTAREA>";
exit();
}
?>

<HTML><BODY>
<TABLE>
<FORM METHOD='POST' ACTION=''>
<TR>
<TD>Click here to read file: </TD>
<TD><INPUT TYPE='submit' NAME='doit' VALUE='Read textfile'></TD>
</TR></FORM>
</TABLE>
</BODY></HTML>

This example shows how to read a textfile and present it in a <TEXTAREA>
Simply put the above code in a php file, and call it from your webserver. Remember you need to have a file named “file.txt” for this example.

To the top ^

How to CREATE a zip archive using PHP

TAGS: PHP, ZIP, Archive

<?php
if (isset($_POST['dozip'])) {
$thedir = getcwd();
$zippath = $thedir . "/" . "zippo.zip";

$zip = new ZipArchive();
if ($zip->open($zippath, ZipArchive::CREATE)) {
$zip->addFile("file_1.txt");
$zip->addFile("file_2.txt");
$zip->addFile("file_3.txt");
}
$zip->close();

echo "Finished"; exit();
}

?>

<HTML><BODY>
<TABLE>
<FORM METHOD='POST' ACTION=''>
<TR>
<TD>Click here to create ZIP archive: </TD>
<TD><INPUT TYPE='submit' NAME='dozip' VALUE='Create zip archive'></TD>
</TR></FORM>
</TABLE>
</BODY></HTML>

This snippet shows how easy it is to create a .zip archive using PHP. Remember to have the three text files present in your directory.

To the top ^

About

TAGS: About, Blog, PHP, Webprogramming

About The level67 E-book

The PHP Snippet Compilation for Beginners.
A Free Ebook from level67.wordpress.com. This version contains 30 easy source snippets, ready to use.
Save it, and keep it in your source library. A good reference for beginners starting PHP webprogramming. A continuing project, to be updated.
This ebook is basicly the content from my blog; http://level67.wordpress.com/
Here you should be able to find newest version of this ebook, in both the E-pub, and AZW3 formats.

For creation of this e-book I've used;
Ubuntu Gnome
Geany
Google Chrome
LibreOffice Writer
Calibre
The GIMP